package PIPL::Configuration::PathTest;

use strict;

use base 'Test::Unit::TestCase';

use PIPL::Configuration::Path;

use Test::MockObject;

sub new {
    my $self = shift()->SUPER::new(@_);
    return $self;
}

sub test_PIPL_Configuration_Path_instance {
    my $self = shift;
    PIPL::Configuration::Path->destroy;
    my $obj1 = PIPL::Configuration::Path->instance;
    my $obj2 = PIPL::Configuration::Path->instance;
    $self->assert_not_null($obj1);
    $self->assert_not_null($obj2);
    $self->assert($obj1->isa('PIPL::Configuration::Path'));
    $self->assert($obj2->isa('PIPL::Configuration::Path'));
    $self->assert_equals($obj1, $obj2);
}

sub test_PIPL_Configuration_Path_confdirs_Unix {
    my $self = shift;

    # set up
    my $home = sprintf('/home/test-%s-%s', time, $$);
    my $perl_pipl_dir = sprintf('/opt/test-%s-%s', time, $$);

    my $orig_Env_Self = $PIPL::System::Env::Self;
    $PIPL::System::Env::Self = {'PERL_PIPL_DIR'=>$perl_pipl_dir};
    my $orig_Filesystem_Self = $PIPL::System::Filesystem::Self;
    $PIPL::System::Filesystem::Self = {'is_Unix'=>1};

    my $mock = Test::MockObject->new;
    $mock->fake_module(
        'File::Spec',
        catdir => sub { shift; return join '/', @_; },
    );
    $mock->fake_module(
        'PIPL::System::Path',
        home => sub { $home; },
    );

    PIPL::Configuration::Path->destroy;

    # main
    my $confdirs = PIPL::Configuration::Path->confdirs;
    $self->assert_not_null($confdirs);
    $self->assert_deep_equals(
        [
            $perl_pipl_dir,
            $home . '/.pi.pl',
            '/etc/pi.pl',
        ], $confdirs);

    # tear down
    $PIPL::System::Env::Self = $orig_Env_Self;
    $PIPL::System::Filesystem::Self = $orig_Filesystem_Self;
    PIPL::Configuration::Path->destroy;
}

sub test_PIPL_Configuration_Path_confdirs_MSWin32 {
    my $self = shift;

    # set up
    my $home = sprintf('C:\Documents and Settings\test-%s-%s', time, $$);
    my $appdata = sprintf('C:\Program Files\Common\test-%s-%s', time, $$);
    my $perl_pipl_dir = sprintf('C:\Program Files\test-%s-%s', time, $$);

    my $orig_Env_Self = $PIPL::System::Env::Self;
    $PIPL::System::Env::Self = {'PERL_PIPL_DIR'=>$perl_pipl_dir};
    my $orig_Filesystem_Self = $PIPL::System::Filesystem::Self;
    $PIPL::System::Filesystem::Self = {'is_Win32'=>1};

    my $mock = Test::MockObject->new;
    $mock->fake_module(
        'File::Spec',
        catdir => sub { shift; return join '\\', @_; },
    );
    $mock->fake_module(
        'PIPL::System::Path',
        home => sub { $home; },
        appdata => sub { $appdata; },
    );

    PIPL::Configuration::Path->destroy;

    # main
    my $confdirs = PIPL::Configuration::Path->confdirs;
    $self->assert_not_null($confdirs);
    $self->assert_deep_equals(
        [
            $perl_pipl_dir,
            $home . '\PI.PL',
            $appdata . '\PI.PL',
        ], $confdirs);

    # tear down
    $PIPL::System::Env::Self = $orig_Env_Self;
    $PIPL::System::Filesystem::Self = $orig_Filesystem_Self;
    PIPL::Configuration::Path->destroy;
}

1;
